package com.ttl.ttldemo.controller;


import com.ttl.ttldemo.config.DirectConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;

import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 延迟消息实现方式一：给消息设置延迟时间
 */
@Slf4j
@RestController
public class SendMessage {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping("/send")
    public String sendMessage(){
        MessageProperties messageProperties=new MessageProperties();
        messageProperties.setContentEncoding("UTF-8");
        //过期时间 ，why not a Date or long（源码写的，他问我）
//        messageProperties.setExpiration("10000");
        Message message= MessageBuilder.withBody("ttl message".getBytes()).andProperties(messageProperties).build();
        rabbitTemplate.convertAndSend(DirectConfig.TTL_EXCHANGE,DirectConfig.TTL_ROUTE_KEY,message);
        log.info("消息发送完毕，发送时间为：{}",new Date());
        return "success";
    }
}
